package Servlet.Admin;

import Entity.MyTableData;

import Entity.Role.Student;
import Entity.Role.Teacher;
import Entity.TitleAndPaper.Title;
import Service.Admin.AccountService;
import Service.Admin.Imp.AccountServiceImp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(urlPatterns = {"/getAllStudent", "/getAllTeacher", "/studentAdd", "/studentDelete", "/teacherAdd", "/teacherDelete", "/studentUpdate", "/teacherUpdate", "/messageAdd"})
public class AccountServlet extends HttpServlet {
    @SneakyThrows
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getAllStudent".equals(url)) {
            getAllStudent(request, response);
        } else if ("/getAllTeacher".equals(url)) {
            getAllTeacher(request, response);
        } else if ("/studentAdd".equals(url)) {
            studentAdd(request, response);
        } else if ("/studentDelete".equals(url)) {
            studentDelete(request, response);
        } else if ("/teacherAdd".equals(url)) {
            teacherAdd(request, response);
        } else if ("/teacherDelete".equals(url)) {
            teacherDelete(request, response);
        } else if ("/studentUpdate".equals(url)) {
            studentUpdate(request, response);
        } else if ("/teacherUpdate".equals(url)) {
            teacherUpdate(request, response);
        } else if ("/messageAdd".equals(url)) {
            messageAdd(request, response);
        }
    }

    /**
     * 得到所有学生信息
     */
    private void getAllStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        MyTableData myTableData = accountService.getAllStudent();
        Object json = JSONObject.toJSON(myTableData);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 得到所有教师信息
     */
    private void getAllTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        MyTableData myTableData = accountService.getAllTeacher();
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 新增学生信息
     */
    private void studentAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String data = request.getParameter("taskData");
        JSONObject json = JSONObject.parseObject(data);
        Student student = new Student();
        student.setSNO(json.get("sNO").toString());
        student.setPassword(json.get("password").toString());
        student.setName(json.get("name").toString());
        student.setSex(json.get("sex").toString());
        student.setTel(json.get("tel").toString());
        student.setEmail(json.get("email").toString());
        student.setCollName(json.get("collName").toString());
        student.setMajorName(json.get("majorName").toString());
        student.setClasses(json.get("classes").toString());
        boolean flag = accountService.studentAdd(student);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 通过学号修改学生信息
     */
    private void studentUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String data = request.getParameter("taskData");
        String sno = request.getParameter("sno");
        JSONObject json = JSONObject.parseObject(data);
        Student student = new Student();
        student.setSNO(json.get("Sedit_sNO").toString());
        student.setPassword(json.get("Sedit_password").toString());
        student.setName(json.get("Sedit_name").toString());
        student.setSex(json.get("Sedit_sex").toString());
        student.setTel(json.get("Sedit_tel").toString());
        student.setEmail(json.get("Sedit_email").toString());
        student.setCollName(json.get("Sedit_collName").toString());
        student.setMajorName(json.get("Sedit_majorName").toString());
        student.setClasses(json.get("Sedit_classes").toString());
        boolean flag = accountService.studentUpdate(student, sno);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 通过学号删除学生信息
     */
    private void studentDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String sno = request.getParameter("sNO");

        boolean flag = accountService.studentDelete(sno);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 新增教师信息
     */
    private void teacherAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String data = request.getParameter("taskData");
        JSONObject json = JSONObject.parseObject(data);
        Teacher teacher = new Teacher();
        teacher.setTNO(json.get("tNO").toString());
        teacher.setPassword(json.get("password").toString());
        teacher.setName(json.get("name").toString());
        teacher.setSex(json.get("sex").toString());
        teacher.setTel(json.get("tel").toString());
        teacher.setEmail(json.get("email").toString());
        teacher.setCollegeId(json.get("collegeId").toString());
        teacher.setPost(json.get("post").toString());
        teacher.setDepartment(json.get("department").toString());
        boolean flag = accountService.teacherAdd(teacher);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 通过工号修改教师信息
     */
    private void teacherUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String data = request.getParameter("taskData");
        String tno = request.getParameter("tno");
        JSONObject json = JSONObject.parseObject(data);
        Teacher teacher = new Teacher();
        teacher.setTNO(json.get("Tedit_tNO").toString());
        teacher.setPassword(json.get("Tedit_password").toString());
        teacher.setName(json.get("Tedit_name").toString());
        teacher.setSex(json.get("Tedit_sex").toString());
        teacher.setTel(json.get("Tedit_tel").toString());
        teacher.setEmail(json.get("Tedit_email").toString());
        teacher.setCollegeId(json.get("Tedit_collegeId").toString());
        teacher.setDepartment(json.get("Tedit_department").toString());
        teacher.setPost(json.get("Tedit_post").toString());
        boolean flag = accountService.teacherUpdate(teacher, tno);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 通过工号删除教师信息
     */
    private void teacherDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String tno = request.getParameter("tNO");

        boolean flag = accountService.teacherDelete(tno);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }


    /**
     * 新增公告信息
     */
    private void messageAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImp();
        String msg_title = request.getParameter("msg_title");
        String msg_from = request.getParameter("msg_from");
        String msg_content = request.getParameter("msg_content");
        boolean flag = accountService.messageAdd(msg_title, msg_from, msg_content);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
